use "nzes0217_analysis.dta", replace


/*Create variables for main analysis*/
gen pvote_coll = partyvote
recode pvote_coll 0=. 7=6 9/14=16 16/99=16
label define partynames 1 Labour 2 National 3 Green 4 NZFirst 5 ACT 6 Maori 8 TOP 15 UFuture 16 Other
label values pvote_coll partynames
gen partyvote3 = partyvote
recode partyvote3 0=. 1=1 2=2 *=3
label define pn3 1 Labour 2 National 3 Other
label values partyvote3 pn3
gen vote_nat = partyvote
recode vote_nat 2=1 1=0 0=. 3/99=0
gen vote_lab = partyvote
recode vote_lab 1=1 0=. 2/99=0
gen genretnm = genret
recode genretnm 1=5 2=4 3=3 4=2 5=1 6/max=3
gen genretm = genret
recode genretm 1=5 2=4 3=3 4=2 5=1 6/max=.
gen genret3 = genret
recode genret3 1/2=1 4/5=-1 *=0
gen econbetter = genret
recode econbetter 1/2=1 *=0
gen econworse = genret
recode econworse 4/5=1 *=0
gen ebtow = .
recode ebtow *=0 if econbetter==1
recode ebtow *=.5 if econbetter==0 & econworse==0
recode ebtow *=1 if econworse==1

gen step1 = nlscale
recode step1 0=4 8=4
gen lr1993 = (((step1-1)/6)*10)+1
drop step1
recode lr1993 .=0
gen lr1996 = cscal + 1
recode lr1996 100/max=6 .=0
gen lr1999 = SCALE + 1
recode lr1999 100=6 .=0
gen lr2002 = wlrscle + 1
recode lr2002 100=6 .=0
gen lr2005 = ylscale + 1
recode lr2005 100=6 .=0
gen lr2008 = zscale + 1
recode lr2008 100=6 .=0
gen lr2011 = jslflr + 1
recode lr2011 100=6 .=0
gen lr2014 = dslflr + 1
recode lr2014 100=6 .=0
gen lr2017 = rselflr + 1
recode lr2017 100=6 .=0
gen lrself_s1 = lr1993 + lr1996 + lr1999 + lr2002 + lr2005 + lr2008 + lr2011 + lr2014 + lr2017
recode lrself_s1 0=6
gen lrself=lrself_s1 - 1
gen lrselfnm = lrself
recode lrselfnm .=5


gen age10 = age/10
recode tradeunion *=1 if rtradeunion==1 | rtradeunion==2 | rtradeunion==3 | jtradeunion==1 | jtradeunion==2 | jtradeunion==3
recode tradeunion *=0 if rtradeunion==4 | rtradeunion==9 | jtradeunion==4 | jtradeunion==9
recode educcoll *=1 if educcolls==1
recode educcoll *=2 if educcolls==2
recode educcoll *=3 if educcolls==3
recode educcoll *=4 if educcolls==4
gen european = 0
recode european *=1 if dethnicfinal==1 | reuroeth==1 | jethnicity_e==1 |  zethn1==1 | YETHMOS==1 | yethmst==1 | yetheur==1 | wethid==10 | SETHNE==1 | nethn==1  | qethn==1
recode european *=0 if rmaineth==2 | zethnic==2 | SETHSTR==2 | SETHSTR==3 | SETHSTR==4

gen partyvote3i = partyvote3
recode partyvote3i 1=2 2=1

gen econret = ebtow
recode econret 0=1 1=0


/*Main Models reported in Table 3*/
mlogit partyvote3i c.econret##i.year lrself female age10 educcoll hhhinc lhhinc tradeunion if didvote==1 & year<2002, b(1)
mlogit partyvote3 c.econret##i.year lrself female age10 educcoll hhhinc lhhinc tradeunion if didvote==1 & year>1999 & year<2011,  b(1)
mlogit partyvote3i c.econret##i.year lrself female age10 educcoll hhhinc lhhinc tradeunion lrself if didvote==1 & year>2008, b(1)

/*Generate predicted probabilities in figure 2*/
qui mlogit partyvote3i c.ebtow##i.year female age10 educcoll hhhinc lhhinc tradeunion lrself if didvote==1 & year<2002, b(1)
margins, dydx(ebtow) at(year=(1993 1996 1999) female=1 age10=5 educcoll=3 hhhinc=0 lhhinc=0 tradeunion=0)
marginsplot, unique yline(0, lc(gs11) lpattern(dash)) recast(scatter) scheme(s1mono) xtitle("") ytitle("") title("A. 1993, 1996, 1999") name(y939699, replace)
qui mlogit partyvote3 c.ebtow##i.year female age10 educcoll hhhinc lhhinc tradeunion lrself if didvote==1 & year>1999 & year<2011, b(1)
margins, dydx(ebtow) at(year=(2002 2005 2008) female=1 age10=5 educcoll=3 hhhinc=0 lhhinc=0 tradeunion=0)
marginsplot, unique yline(0, lc(gs11) lpattern(dash)) recast(scatter) scheme(s1mono) xtitle("") ytitle("") title("B. 2002, 2005, 2008") name(y020508, replace)
qui mlogit partyvote3i c.ebtow##i.year female age10 educcoll hhhinc lhhinc tradeunion lrself if didvote==1 & year>2008, b(1)
margins, dydx(ebtow) at(year=(2011 2014 2017) female=1 age10=5 educcoll=3 hhhinc=0 lhhinc=0 tradeunion=0)
marginsplot, unique yline(0, lc(gs11) lpattern(dash)) recast(scatter) scheme(s1mono) xtitle("") ytitle("") title("C. 2011, 2014, 2017") name(y111417, replace)
graph combine y939699 y020508 y111417, r(1) ycommon scheme(s1mono)


/*create additional instruments for CF estimator models*/
gen polint93 = nintrst
recode polint93 5=0 .=0
gen polint96 = pintrst
recode polint96 .=0
gen polint99 = SINTRST
recode polint99 .=0
gen polint02 = wintrst
recode polint02 .=0
gen polint05 = yintrst
recode polint05 .=0
gen polint08 = zintrst
recode polint08 .=0
gen polint11 = jinterest
recode polint11 9=4 .=0
gen polint14 = dinterest
recode polint14 .=0
gen polint17 = rinterest
recode polint17 .=0
gen polinterest = polint93 + polint96 + polint99 + polint02 + polint05 + polint08 + polint11 + polint14 + polint17
recode polinterest 0=.

gen unemployed = 0
recode unemployed 0=1 if nwork==4 | pnwork==4 | SNWORKU==1 | wnworku ==1 | ywkun==1 | zunemp==1 | jwkun==1 | dwkun==1 | rempstunemp==1


gen personalecon93_99 = .
recode personalecon93_99 *=1 if nrnow==1 | prnow==1 | SRNOW==1
recode personalecon93_99 *=2 if nrnow==2 | prnow==2 | SRNOW==2
recode personalecon93_99 *=3 if nrnow==3 | prnow==3 | SRNOW==3
recode personalecon93_99 *=4 if nrnow==4 | prnow==4 | SRNOW==4
recode personalecon93_99 *=5 if nrnow==5 | prnow==5 | SRNOW==5

gen hou02 = wfnho
recode hou02 9=0 .=0
gen hou05 = yhounow
recode hou05 9=0 .=0
gen hou08 = zrethou
recode hou08 9=0 .=0
gen personalecon02_08 = hou02 + hou05 + hou08
recode personalecon02_08 0=.

gen per11 = jimpsol
recode per11 9=0 .=0
gen per14 = dimpsol
recode per14 9=0 .=0
gen per17 = rimpsol
recode per17 9=0 .=0
gen personalecon11_17 = per11 + per14 + per17
recode personalecon11_17 0=.

/*CF estimator approach reported in Table A3 of SI*/
program nz01
reg econret female age10 educcoll hhhinc lhhinc tradeunion european polinterest personalecon93_99 unemployed lrself i.year if didvote==1 & year<2002
predict econret_r1, residuals
mlogit partyvote3i lrself c.econret_r1##i.year if didvote==1 & year<2002, b(1)
drop econret_r1
end
bootstrap, reps(200): nz01


program nz02
reg econret female age10 educcoll hhhinc lhhinc tradeunion european polinterest personalecon02_08 unemployed lrself i.year if didvote==1 & year>1999 & year<2011
predict econret_r2, residuals
mlogit partyvote3 lrself c.econret_r2##i.year if didvote==1 & year>1999 & year<2011, b(1)
drop econret_r2
end
bootstrap, reps(200): nz02


program nz03
reg econret female age10 educcoll hhhinc lhhinc tradeunion european polinterest personalecon11_17 unemployed lrself i.year if didvote==1 & year>2008
predict econret_r3, residuals
mlogit partyvote3i lrself c.econret_r3##i.year if didvote==1 & year>2008, b(1)
drop econret_r3
end
bootstrap, reps(200): nz03
